package com.imast.android.bys.util;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.imast.android.bys.Config;

public class SoapClient {
	
	private SoapObject request;
	private String soapAction;
	
	public SoapObject getRequest() {
		return request;
	}

	public void setRequest(SoapObject request) {
		this.request = request;
	}

	public String getSoapAction() {
		return soapAction;
	}

	public void setSoapAction(String soapAction) {
		this.soapAction = soapAction;
	}
	
	public SoapClient(String methodName, String soapAction) {
		this.request = GetSoapObject(methodName);
        this.soapAction = soapAction;
	}
	
	public SoapObject GetSoapObject(String MethodName)
    {
        return new SoapObject(Config.NAMESPACE,MethodName);
    }
    public SoapSerializationEnvelope GetEnvelope(SoapObject Soap)
    {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Soap);
        return envelope;
    }
    
    public SoapPrimitive makeCallSoapPrimitive()
    {
    	HttpTransportSE transporte = new HttpTransportSE(Config.URL);
         try {
        	 
        	 SoapSerializationEnvelope envelope = GetEnvelope(getRequest());
        	 
        	 transporte.call(getSoapAction(), envelope);

        	 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)envelope.getResponse();
             return resultsRequestSOAP;
         }
         catch(Exception e)
         {
             e.printStackTrace();
             
         }
         return null;
    }
    
    public SoapObject makeCallSoapObject()
    {
    	HttpTransportSE transporte = new HttpTransportSE(Config.URL);
    	transporte.debug = true;
         try {
        	 
        	 SoapSerializationEnvelope envelope = GetEnvelope(getRequest());
        	 transporte.call(getSoapAction(), envelope);
        	 
        	 //Log.i("SOAPOBJECT", transporte.responseDump);
        	 SoapObject response = (SoapObject)envelope.getResponse();
             return response;
         }
         catch(Exception e)
         {
             e.printStackTrace();
             
         }
         return null;
    }

}
